#include "s60remote.h"

S60Remote::S60Remote(QObject* parent) : QObject(parent) {
    setObjectName("S60Remote");
    m_interfaceSelector = CRemConInterfaceSelector::NewL();
    m_coreTarget = CRemConCoreApiTarget::NewL(*m_interfaceSelector, *this);
    m_interfaceSelector->OpenTargetL();
}

S60Remote::~S60Remote() {
    delete m_coreTarget;  // not sure if this is necessary
    delete m_interfaceSelector;
}

// ----------------------------------------------------------------------------
// MrccatoCommand()
// Receives events (press/click/release) from the following buttons:
// Play/Pause, Volume Up, Volume Down, Stop, next, previous, rewind, forward
// ----------------------------------------------------------------------------
void S60Remote::MrccatoCommand(TRemConCoreApiOperationId aOperationId,
    TRemConCoreApiButtonAction aButtonAct) {
    TRequestStatus status;

    switch( aOperationId ) {
        case ERemConCoreApiPausePlayFunction:  {
            if (aButtonAct == ERemConCoreApiButtonClick) {
                emit playPause();
            }
            //Send the response back to Remcon server
            m_coreTarget->PausePlayFunctionResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }
        case ERemConCoreApiStop: {
            if (aButtonAct == ERemConCoreApiButtonClick)  {
                emit stop();
            }
            m_coreTarget->StopResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }
        // next track
        case ERemConCoreApiForward: {
            if (aButtonAct == ERemConCoreApiButtonClick)  {
                emit next();
            }            
            m_coreTarget->ForwardResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }
        // previous track
        case ERemConCoreApiBackward: {
            if (aButtonAct == ERemConCoreApiButtonClick)  {
                emit previous();
            }
            else if (aButtonAct == ERemConCoreApiButtonPress)  {
                emit rewindPressed();
            }
            else if (aButtonAct == ERemConCoreApiButtonRelease)  {
                emit rewindReleased();
            }
            m_coreTarget->BackwardResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }
        case ERemConCoreApiFastForward: {
            if (aButtonAct == ERemConCoreApiButtonPress)  {
                emit fastForwardPressed();
            }
            else if (aButtonAct == ERemConCoreApiButtonRelease)  {
                emit fastForwardReleased();
            }
            m_coreTarget->ForwardResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }
        case ERemConCoreApiRewind: {
            if (aButtonAct == ERemConCoreApiButtonPress)  {
                emit rewindPressed();
            }
            else if (aButtonAct == ERemConCoreApiButtonRelease)  {
                emit rewindReleased();
            }
            m_coreTarget->ForwardResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }
        case ERemConCoreApiVolumeUp: {
            if (aButtonAct != ERemConCoreApiButtonRelease)  {
                emit volumeUp();
            }
            m_coreTarget->VolumeUpResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }
        case ERemConCoreApiVolumeDown: {
            if (aButtonAct != ERemConCoreApiButtonRelease)  {
                emit volumeDown();
            }
            m_coreTarget->VolumeDownResponse(status, KErrNone);
            User::WaitForRequest(status);
            break;
        }

        default:
            break;
    }
}

